package com.tanhua.manage.controller;

import com.tanhua.manage.service.PublishInfoService;
import com.tanhua.manage.vo.PageResultVo;
import com.tanhua.manage.vo.PublishInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//消息模块controller
@RestController
@RequestMapping("/manage/messages")
public class PublishInfoController {
    @Autowired
    private PublishInfoService publishInfoService;

    //审核通过
    @RequestMapping("pass")
    private ResponseEntity<Map<String, String>> pass(@RequestHeader("Authorization") String token, @RequestBody List<String> publishIds) {
        Map<String, String> map = new HashMap<>();
        try {
            Boolean res = publishInfoService.pass(token, publishIds);
            if (res) {
                map.put("message", "通过成功");
                return ResponseEntity.ok(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("message", "通过失败");
        return ResponseEntity.status(500).body(map);
    }

    //审核拒绝
    @RequestMapping("reject")
    private ResponseEntity<Map<String, String>> reject(@RequestHeader("Authorization") String token, @RequestBody List<String> publishIds) {
        Map<String, String> map = new HashMap<>();
        try {
            Boolean res = publishInfoService.reject(token, publishIds);
            if (res) {
                map.put("message", "拒绝成功");
                return ResponseEntity.ok(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("message", "拒绝失败");
        return ResponseEntity.status(500).body(map);
    }

    //审核撤销
    @RequestMapping("revocation")
    private ResponseEntity<Map<String, String>> revocation(@RequestHeader("authorization") String token, @RequestBody List<String> publishIds) {
        Map<String, String> map = new HashMap<>();
        try {
            Boolean res = publishInfoService.revocation(token, publishIds);
            if (res) {
                map.put("message", "撤销成功");
                return ResponseEntity.ok(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("message", "撤销失败");
        return ResponseEntity.status(500).body(map);
    }

    //置顶
    @PostMapping("/{id}/top")
    private ResponseEntity<Map<String, String>> top(@PathVariable("id") String publishId, @RequestHeader("Authorization") String token) {
        Map<String, String> map = new HashMap<>();
        try {
            Boolean res = publishInfoService.top(publishId, token);
            if (res) {
                map.put("message", "置顶成功");
                return ResponseEntity.ok(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("message", "置顶失败");
        return ResponseEntity.status(500).body(map);
    }

    //取消置顶
    @PostMapping("/{id}/untop")
    private ResponseEntity<Map<String, String>> untop(@PathVariable("id") String publishId, @RequestHeader("Authorization") String token) {
        Map<String, String> map = new HashMap<>();
        try {
            Boolean res = publishInfoService.untop(publishId, token);
            if (res) {
                map.put("message", "取消置顶成功");
                return ResponseEntity.ok(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("message", "取消置顶失败");
        return ResponseEntity.status(500).body(map);
    }

    //消息详情
    @GetMapping("/{id}")
    private ResponseEntity<PublishInfoVo> getPublishInfo(@PathVariable("id") String publishId, @RequestHeader("Authorization") String token) {
        try {
            PublishInfoVo publishInfoVo = publishInfoService.getPublishInfo(publishId, token);
            if (publishInfoVo != null) {
                return ResponseEntity.ok(publishInfoVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(500).body(null);
    }

    //消息翻页
    @RequestMapping
    private ResponseEntity<PageResultVo> messages(@RequestParam(value = "pagesize", required = false, defaultValue = "10") Integer pageSize,
                                                  @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                                  @RequestParam(value = "sortProp", required = true) String sortProp,
                                                  @RequestParam(value = "sortOrder", required = true) String sortOrder,
                                                  @RequestParam(value = "id", required = false) String id,
                                                  @RequestParam(value = "sd", required = false) Long sd,
                                                  @RequestParam(value = "ed", required = false) Long ed,
                                                  @RequestParam(value = "uid", required = false) Long uid,
                                                  @RequestParam(value = "state", required = true) Integer state) {
        try {
            PageResultVo pageResult = publishInfoService.messages(pageSize, page, sortProp, sortOrder, id, sd, ed, uid, state);
            if (pageResult != null) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(500).body(null);
    }
}
